package obstacle;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;

import Vue.Terrain;

import reseau.Coordonnees2D;
import reseau.Vecteur2D;

public class Ligne implements IObstacle 
{
	protected Coordonnees2D point1, point2;
	protected Vecteur2D vecteurTangent;
	protected Terrain _terrain;
	public Ligne(Coordonnees2D point1, Coordonnees2D point2, Terrain terrain)
	{
		this.point1 = point1;
		this.point2 = point2;
		this.vecteurTangent = new Vecteur2D(point1, point2);
		this._terrain = terrain;
	}
	
	public Coordonnees2D getIntersection(Coordonnees2D depart, Coordonnees2D arrivee) 
	{
		return ObstaclePolygone.intersection(depart, arrivee, this.point1, this.point2);
	}

	@Override
	public Vecteur2D getVecteurTangent(Coordonnees2D depart, Coordonnees2D arrivee) 
	{
		if(this.getIntersection(depart, arrivee)!=null)
			return this.vecteurTangent;
		return null;
	}

	public void display(Graphics2D g)
	{
		Polygon polygon = new Polygon();
		polygon.addPoint((int)point1.getX(), (int)point1.getY());
		polygon.addPoint((int)point2.getX(), (int)point2.getY());
		int px1 = (point1.getX() > point2.getX())? (int)point2.getX():(int)point1.getX();
		int px2 = (point1.getX() > point2.getX())? (int)point1.getX():(int)point2.getX();
		int py1 = (point1.getY() > point2.getY())? (int)point2.getY():(int)point1.getY();
		int py2 = (point1.getY() > point2.getY())? (int)point1.getY():(int)point2.getY();
		for(int i=px1; i<px2; i++)
		{
			for(int j=py1; j<py2; j++)
			{
				if(polygon.contains(i, j))
				{
					this._terrain.initBuffer(i, j, 51, 85, 204);
					
				}
			}
		}
			
//		g.setColor(Color.BLACK);
//		g.drawLine((int)point1.getX(), (int)point1.getY(), (int)point2.getX(), (int)point2.getY());
	}

	@Override
	public boolean contient(Coordonnees2D point) {
		Polygon polygon = new Polygon();
		polygon.addPoint((int)point1.getX(), (int)point1.getY());
		polygon.addPoint((int)point2.getX(), (int)point2.getY());
		if(polygon.contains(point.getX(), point.getY()))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	@Override
	public boolean tue() {
		// TODO Auto-generated method stub
		return false;
	}
}
